﻿namespace Cmn.Cls
{
    using Cmn;
    using System;
    using System.IO;
    using System.Web;
    using System.Web.UI.WebControls;

    public class Upload
    {
        private string _allowedExtensions = ".gif|.png|.jpg|.bmp";
        private string _fileName = "";
        private string _filePath = "~/";
        private int _fileSize = 0;
        private bool _isCreateFolderForNotExits = false;
        private double _maxFileSize = 4000.0;
        private RandFileType _RandFileType = RandFileType.None;
        private int _RandNumbers = 5;
        private int _State = 0;

        public void doUpLoad(FileUpload _fileUpload)
        {
            string path = HttpContext.Current.Server.MapPath(this._filePath);
            if (!Directory.Exists(path))
            {
                if (this._isCreateFolderForNotExits)
                {
                    FileFolder.CreateFolder(this._filePath);
                }
                else
                {
                    this._State = 3;
                    return;
                }
            }
            if ((_fileUpload.PostedFile.ContentLength / 0x400) > this._maxFileSize)
            {
                this._State = 4;
            }
            else
            {
                string str2 = "";
                switch (this._RandFileType)
                {
                    case RandFileType.None:
                        str2 = "";
                        break;

                    case RandFileType.FileName_DateTime:
                        str2 = "_" + Rand.RndDateStr();
                        break;

                    case RandFileType.FileName_RandNumber:
                        str2 = "_" + Rand.RndCode(this._RandNumbers);
                        break;
                }
                bool flag = false;
                if (_fileUpload.HasFile)
                {
                    string str3 = Path.GetExtension(_fileUpload.FileName).ToLower();
                    string[] strArray = this._allowedExtensions.Split(new string[] { "|" }, StringSplitOptions.None);
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (str3 == strArray[i])
                        {
                            flag = true;
                        }
                    }
                    if (flag)
                    {
                        try
                        {
                            _fileUpload.SaveAs(path + Path.GetFileNameWithoutExtension(_fileUpload.FileName) + str2 + Path.GetExtension(_fileUpload.FileName));
                            this._State = 0;
                            this._fileSize = _fileUpload.PostedFile.ContentLength / 0x400;
                            this._fileName = this._filePath + Path.GetFileNameWithoutExtension(_fileUpload.FileName) + str2 + Path.GetExtension(_fileUpload.FileName);
                        }
                        catch
                        {
                            this._State = 2;
                        }
                    }
                    else
                    {
                        this._State = 1;
                    }
                }
                else
                {
                    this._State = 2;
                }
            }
        }

        public string AllowedExtensions
        {
            set
            {
                this._allowedExtensions = value;
            }
        }

        public string FileName
        {
            get
            {
                return this._fileName;
            }
        }

        public string FilePath
        {
            set
            {
                this._filePath = value;
            }
        }

        public int FileSize
        {
            get
            {
                return this._fileSize;
            }
        }

        public bool isCreateFolderForNotExits
        {
            set
            {
                this._isCreateFolderForNotExits = value;
            }
        }

        public int MaxFileSize
        {
            set
            {
                this._maxFileSize = value;
            }
        }

        public int RandNumbers
        {
            set
            {
                this._RandNumbers = value;
            }
        }

        public RandFileType RndFileType
        {
            set
            {
                this._RandFileType = value;
            }
        }

        public int State
        {
            get
            {
                return this._State;
            }
        }

        public enum RandFileType
        {
            None,
            FileName_DateTime,
            FileName_RandNumber
        }
    }
}

